<?php
declare (strict_types=1);

namespace app\api\controller;


use app\model\UserCouponModel;
use app\model\CouponModel;
use app\model\UserModel;
use think\facade\Request;
use think\facade\Validate;

class UserCoupon
{
    //获取个人优惠卷列表
    public function list()
    {

        $userCouponList = UserCouponModel::where('user_coupon_id', '>', 0)->select();
        
        foreach ($userCouponList as &$value){
            //优惠卷名称
            $couponOne = CouponModel::find($value['coupon_id']);
            // $value['coupon'] = $couponOne;
            $value['full'] = $couponOne['full'];
            $value['reduce'] = $couponOne['reduce'];
            //用户昵称
            $userOne = UserModel::where('open_id','=',$value['open_id'])->find();
            $value['nickname'] = $userOne['nickname'];
            //优惠卷状态
            if($value['coupon_status'] == 1){
                $value['coupon_status_name'] = '待使用';
            }else if($value['coupon_status'] == 2){
                $value['coupon_status_name'] = '已使用';
            }else if($value['coupon_status'] == 3){
                $value['coupon_status_name'] = '已过期';
            }
        }
        unset($value);
        $data = [
            'status' => 0,
            'message' => '',
            'data' => [
                'userCouponList' => $userCouponList
            ],
        ];
        return json($data);
    }
    
    public function my()
    {
        $openId = Request::param("open_id");
        
        $validate = Validate::rule([
            'open_id|用户id' => 'require',
        ]);
        
        if (!$validate->check(['open_id' => $openId])) {
            $data = [
                'status' => 10001,
                'message' => $validate->getError(),
                'data' => [],
            ];
            return json($data);
        }
        
        $couponList=CouponModel::select();
        $userCouponList = UserCouponModel::where("open_id","=",$openId)->select();
        
        foreach ($couponList as &$value){
            //用户昵称
            $userOne = UserModel::where('open_id','=',$openId)->find();
            $value['nickname'] = $userOne['nickname'];
            
            $userCouponOne = UserCouponModel::where("open_id","=",$openId)->where("coupon_id","=",$value['coupon_id'])->find();
            if($userCouponOne){
                $value['user_status'] = 0;
                if(time() >= $userCouponOne['user_coupon_end_time']){
                    $userCouponOne['coupon_status'] = $value['coupon_status'] = 3;
                    $userCouponOne['update_time'] = time();
                    $result = $userCouponOne->save();
                }else{
                    $value['coupon_status'] = $userCouponOne['coupon_status'];
                }
                $value['user_coupon_end_time'] = $userCouponOne['user_coupon_end_time'];
            }else{
                $value['user_status'] = 1;
            }

        }
        unset($value);
        $data = [
            'status' => 0,
            'message' => '',
            'data' => [
                'couponList' => $couponList
            ],
        ];
        return json($data);
    }

    //个人优惠卷增加
    public function add()
    {
        //order增加菜品
        $params = Request::param();
        
        $validate = Validate::rule([
            'coupon_id|优惠卷id' => 'require',
            'open_id|用户id' => 'require',
        ]);
        if (!$validate->check($params)) {
            $data = [
                'status' => 10002,
                'message' => $validate->getError(),
                'data' => []
            ];
            return json($data);
        }
        
         $userCouponOne = UserCouponModel::where("open_id","=",$params['open_id'])->where("coupon_id","=",$params['coupon_id'])->find();
         $couponOne = CouponModel::where("coupon_id","=",$params['coupon_id'])->find();
         if(!$userCouponOne){
            $params['user_coupon_end_time']=time()+$couponOne['day']*86400;
            $params['add_time']=$params['update_time']=time();
            $result=UserCouponModel::create($params);   
         }else{
             $result = false;
         }

        $data = [
            'status' => $result ? 0 : 10001,
            'message' => $result ? '' : '修改数据库失败',
            'data' => [
                'result' =>$result ? true : false
            ]
        ];
        return json($data);
    }
    // //优惠卷删除
    // public function delete()
    // {
    //     $couponId = Request::param("coupon_id");
    //     $validate = Validate::rule([
    //         'coupon_id|优惠券id' => 'require|between:1,' . PHP_INT_MAX,
    //     ]);
    //     if (!$validate->check(['coupon_id' => $couponId])) {
    //         $data = [
    //             'status' => 10002,
    //             'message' => $validate->getError(),
    //             'data' => []
    //         ];
    //         return json($data);
    //     }

    //     $result = CouponModel::destroy($couponId);

    //     $data = [
    //         'status' => $result ? 0 : 10001,
    //         'message' => $result ? '' : '修改数据库失败',
    //         'data' => [
    //             'result' =>$result ? true : false
    //         ]
    //     ];
    //     return json($data);
    // }
    //优惠卷修改
    public function update()
    {

        $params = Request::param();
        $validate = Validate::rule([
            'coupon_id|优惠卷id' => 'require',
            'open_id|用户id' => 'require',
            'coupon_status|优惠卷状态' => 'require'
        ]);
        if (!$validate->check($params)) {
            $data = [
                'status' => 10002,
                'message' => $validate->getError(),
                'data' => []
            ];
            return json($data);
        }

        $userCouponOne = UserCouponModel::where('coupon_id','=',$params['coupon_id'])->where('open_id','=',$params['open_id'])->find();

        if ($params['coupon_status']){
            $userCouponOne['coupon_status']=$params['coupon_status'];
        }
        
        $couponOne['update_time'] = time();
        $result = $userCouponOne->save();

        $data = [
            'status' => $result ? 0 : 10001,
            'message' => $result ? '' : '修改数据库失败',
            'data' => [
                'result' => $result ? true : false
            ]
        ];
        return json($data);
    }
}
